package io.cloud.tec.project.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-requestmapping.html#mvc-ann-requestmapping-uri-templates
 *
 * PathPattern（从SpringMVC v5.3可用，从从SpringMVC v6.0为默认启用）
 *
 * AntPathMatcher
 */
@RestController
@RequestMapping("mvc/{subPath}")
public class SpringMvcUriWeb {
    /**
     * uri-templates
     */

    /**
     * path变量
     * @param accountName
     * @return
     */
    @GetMapping("pathVariable1/account/{accountName}")
    public String pathVariable1(@PathVariable String accountName) {
        return accountName;
    }

    /**
     * path变量-指定变量名称
     * @param pathVariable
     */
    @GetMapping("pathVariable2/account/{accountName}")
    public String pathVariable2(@PathVariable("accountName") String name) {
        return name;
    }

    /**
     * path变量-多个path变量，按顺序取值
     * @param type
     * @param accountName
     */
    @GetMapping("pathVariable3/{type}/{accountName}")
    public Map<String,String> pathVariable3(@PathVariable String type, @PathVariable String accountName) {
        Map<String,String> result = new HashMap<>();
        result.put(type, accountName);
        result.put("a", "b");
        return result;
    }

    /**
     * path变量-指定变量名称：编译参数：-parameters
     * @param type
     * @param accountName
     */
    @GetMapping("pathVariable4/{type}/{accountName}")
    public void pathVariable4(@PathVariable("type") String type, @PathVariable("accountName") String accountName) {

    }

    /**
     * path变量-类级别的path变量、方法级别的path变量
     * @param subPath
     * @param accountName
     */
    @GetMapping("pathVariable5/account/{accountName}")
    public void pathVariable5(@PathVariable String subPath,@PathVariable String accountName) {

    }

    /**
     * path变量-正则表达式：{varName:regex}
     * @param fileName
     * @param timestamp
     * @param ext
     */
    @GetMapping("pathVariable6/{fileName:[a-z-]+]}-{timestamp:[0-9]+]}{ext:\\.[a-z]+]}")
    public void pathVariable6(@PathVariable String fileName,@PathVariable String timestamp, String  ext) {

    }

    /**
     * path变量-通配符
     */
    @GetMapping("pathVariable7/jpe?g")
    public void pathVariable7() {

    }

    /**
     * path变量-通配符
     */
//    @GetMapping("pathVariable8/**/ext")
    public void pathVariable8() {

    }

    /**
     * path变量-占位符
     */
    @GetMapping("pathVariable9/${spring.application.name}")
    public void pathVariable9() {

    }
    @GetMapping(path = "/pets/{petId}", params = "accountName")
    public void paramsExist(@PathVariable String petId) {
        // ...
    }

    @GetMapping(path = "/pets/{petId}", params = "!accountName")
    public void paramsNotExist(@PathVariable String petId) {
        // ...
    }


    @GetMapping(path = "/pets/{petId}", params = "accountName=anonymous")
    public void params(@PathVariable String petId) {
        // ...
    }
    @GetMapping(path = "/pets/{petId}", headers = "accountName")
    public void headersExist(@PathVariable String petId) {
        // ...
    }

    @GetMapping(path = "/pets/{petId}", headers = "!accountName")
    public void headersNotExist(@PathVariable String petId) {
        // ...
    }


    @GetMapping(path = "/pets/{petId}", headers = "accountName=anonymous")
    public void headers(@PathVariable String petId) {
        // ...
    }
}
